<?php
/**
 * 用户操作控制器
 * 
 */
class userAction extends apiAction {
	
	/**
	 * 用户登录
	 */
	public function login(){
		$username = I('username'); 
		$password = I('password');
		
		$user = D('admin')->where(array('userName'=>$username))->find();
		// 较验用户名
		if(! $user){
			$this->ajaxReturn(300, L('no_this_user'));
		}
		// 比较密码
		if(md5($password) !== $user['passWord']){
			$this->ajaxReturn(300, L('password_is_error'));
		}
		//用户详细信息
		$userInfo = D('admin')->get_user_info($user['id']);
        //print_r($userInfo);
		//保存用户信息到session
		session('user', $userInfo);
		
		$data['sessionId']	= session_id(); //会话id
		$data['userId']		= $userInfo['id']; //用户id
		$data['userName']	= $userInfo['username']; //用户名
		$data['realName']	= $userInfo['realName']; //真实改名
		$data['roleId']		= $userInfo['roleId'];	//角色id
		$data['phone']		= $userInfo['phone']; //联系电话
		$data['company']	= $userInfo['company']; //所属劳务公司
		$data['jobPost']	= $userInfo['jobPost']; //工作岗位
		$data['sim']		= $userInfo['sim']; //Sim 卡号
		$data['simSn']		= $userInfo['simSn']; //Sim 卡号对应编码
		$data['imei']		= $userInfo['imei']; //Pad对应IMEI
		$data['icon']		= D('admin')->getIcon($userInfo['id']); //用户头像
		
		if(count($userInfo['areaId']) == 1){
			$data['areaId'] = $userInfo['areaId'][0];
		}
		
		if(count($userInfo['cityId']) == 1){
			$data['cityId'] = $userInfo['cityId'][0];
		}
		
		if(count($userInfo['microAreaId']) == 1){
			$data['microAreaId'] = $userInfo['microAreaId'][0];
		}
		
		if(count($userInfo['storeId']) == 1){
			$data['storeId'] = $userInfo['storeId'][0];
		}
		
		$this->ajaxReturn(200, L('login_success'), $data);
	}
	
	/**
	 * 用户注销
	 */
	public function logout(){
		// 清空当前的session
		session(null); 
		$this->ajaxReturn(200, L('logout_success'));
	}
	
	/**
	 * 我的个人信息
	 */
	public function info(){
		// 个人信息，个人姓名、所在地市、片区、厅店、职位
		$field = array(
			'admin.realName',
			'admin.jobPost',
			'admin.email',
			'admin.phone',
			'city.name'=>'cityName',
			'microArea.name'=>'microAreaName',
			'store.name'=>'storeName',
		);
		$data = D('admin')->alias('admin')->field($field)
				->join(C('DB_PREFIX')."store AS store ON store.id = admin.storeId")
				->join(C('DB_PREFIX')."admin_role AS role ON role.id = admin.roleId")
				->join(C('DB_PREFIX')."region AS microArea ON microArea.id = admin.regionId")
				->join(C('DB_PREFIX')."region AS city ON city.id = microArea.pid")
				->where(array('admin.id'=>$this->user['id']))
				->find();
		
		$this->ajaxReturn(200, L('login_success'), $data);
	}
	
	/**
	 * 修改个人资料
	 */
	public function update(){
		$newPassword = I('newPassword');
		$confirmPassword = I('confirmPassword');
		$email = I('email');
		$phone = I('phone');
		
		$data = array();
		
		if($newPassword){
			if($newPassword == $confirmPassword){
				$data['password'] = md5($newPassword);
			}else{
				$this->ajaxReturn(300, '两次输入的密码不一致');
			}
		}
		
		if($email){
			$data['email'] = $email;
		}
		
		if($phone){
			$data['phone'] = $phone;
		}
		
		if($data){
			D('admin')->where(array('id'=> $this->user['id']))->save($data);
			$this->ajaxReturn(200, L('success'));
		}else{
			$this->ajaxReturn(300, L('error'));
		}
	}
}

/* End of file userAction.class.php */